#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
Mat bgimage;
const char* drawdemo_win = "draw shapes and text demo";

void MyLines();
void MyRectangle();
void MyEllipse();
void MyCircle();
void MyPolygon();
void RandomLineDemo();

int main() {
  Mat src = imread("../images/2.jpg");
  Mat dst;
  if (src.empty()) {
    cout << "could not load image.." << endl;
    return -1;
  }
  bgimage = src.clone();
  // MyLines();
  // MyRectangle();
  // MyEllipse();
  // MyCircle();
  // MyPolygon();

  // putText(bgimage, "hello.cxh", Point(300, 300),
  // CV_FONT_HERSHEY_COMPLEX, 1.0,
  //         Scalar(0, 0, 0), 1, 8);
  RandomLineDemo();
  namedWindow("opencv setup demo", CV_WINDOW_AUTOSIZE);
  imshow("opencv setup demo", bgimage);

  waitKey(0);
  return 0;
}

void MyLines() {
  Point p1 = Point(20, 30);
  Point p2;
  p2.x = 400;
  p2.y = 400;
  Scalar color = Scalar(0, 0, 255);
  line(bgimage, p1, p2, color, LINE_AA);
}

void MyRectangle() {
  Rect rect = Rect(200, 100, 300, 300);
  Scalar color = Scalar(255, 0, 0);
  rectangle(bgimage, rect, color, 2, LINE_8);
}

void MyEllipse() {
  Scalar color = Scalar(0, 255, 0);
  Point center = Point(bgimage.cols / 4, bgimage.rows / 4);
  Point center2 = Point(bgimage.cols / 4, bgimage.rows / 4);
  ellipse(bgimage, center, center2, 45, 0, 360, color, 2, LINE_8);
}

void MyCircle() {
  Scalar color = Scalar(0, 255, 255);
  Point center = Point(bgimage.cols / 2, bgimage.rows / 2);
  circle(bgimage, center, 150, color, 2, 8);
}

void MyPolygon() {
  Point pts[1][5];
  pts[0][0] = Point(100, 100);
  pts[0][1] = Point(100, 200);
  pts[0][2] = Point(200, 200);
  pts[0][3] = Point(200, 100);
  pts[0][4] = Point(100, 100);

  const Point* ppts[] = {pts[0]};
  int npt[] = {5};

  Scalar color = Scalar(255, 12, 255);
  fillPoly(bgimage, ppts, npt, 1, color, 8);
}

void RandomLineDemo() {
  RNG rng(12345);  // 随机数 + 种子
  Point pt1;
  Point pt2;

  Mat bg = Mat::zeros(bgimage.size(), bgimage.type());
  namedWindow("random line demo", CV_WINDOW_AUTOSIZE);

  for (int i = 0; i < 100; i++) {
    pt1.x = rng.uniform(0, bgimage.cols);
    pt2.x = rng.uniform(0, bgimage.cols);
    pt1.y = rng.uniform(0, bgimage.rows);
    pt2.y = rng.uniform(0, bgimage.rows);
    Scalar color =
        Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));

    if (waitKey(1) > 0) {
      break;
    }
    line(bg, pt1, pt2, color, 1, 8);
    imshow("random line demo", bg);
  }
}